package com.uenpay.im.ui.old.activity.group;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.util.app.base.BaseActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 编辑群信息
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class EditGroupInfoActivity extends BaseActivity {

    private EditText edit_group_info;
    private View loading;
    private TextView intro;
    private int type = -1;
    private String oldStr = "", chat_id = "";
    private GroupMangerHandler mGroupMangerHandler = new GroupMangerHandler();
    private int maxLeght = 26;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_edit_group_info_layout;
    }

    @Override
    public void initView(View view) {
        oldStr = getIntent().getStringExtra("oldStr");
        chat_id = getIntent().getStringExtra("chat_id");
        type = getIntent().getIntExtra("type", -1);
        initView();
        if (type == 1 || type == 2) {
            maxLeght = 16;
        }
        initListener();
    }

    protected void initView() {

        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.save));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        edit_group_info = (EditText) findViewById(R.id.edit_group_info);
        intro = (TextView) findViewById(R.id.intro);
        loading = findViewById(R.id.loading);
        if (type == 1) {
            ((TextView) findViewById(R.id.title_textview))
                    .setText(getResources().getString(
                            R.string.update_group_name));
            intro.setText(getResources().getString(R.string.group_name));
        } else if (type == 2) {
            ((TextView) findViewById(R.id.title_textview))
                    .setText(getResources()
                            .getString(R.string.my_name_in_group));
        } else if (type == 3) {
            ((TextView) findViewById(R.id.title_textview))
                    .setText(getResources().getString(R.string.group_introduce));
            intro.setText(getResources().getString(R.string.group_introduce));
        }

        if (!TextUtils.isEmpty(oldStr)) {
            edit_group_info.setText(oldStr);
            edit_group_info.setSelection(oldStr.length());
        }
        Utils.showSoftKeyboard(edit_group_info);
    }

    protected void initListener() {
        edit_group_info.setFilters(new InputFilter[]{emojiFilter});
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(edit_group_info);
                        finish();
                    }
                });
        edit_group_info.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                if (type == 1 || type == 2) {
                    if (arg0.toString().length() > maxLeght) {
                        String str = arg0.toString().substring(0, maxLeght);
                        edit_group_info.setText(str);
                        edit_group_info.setSelection(edit_group_info.length());
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        String name = edit_group_info.getText().toString();
                        if (!TextUtils.isEmpty(name)
                                && !TextUtils.isEmpty(chat_id)) {
                            if (TextUtils.isEmpty(name.trim())) {
                                IMApplication
                                        .showToast(getResources().getString(
                                                R.string.group_name_not_null));
                                return;
                            }
                            if (type == 1) {
                                if (name.length() > 10) {
                                    IMApplication
                                            .showToast(getResources()
                                                    .getString(
                                                            R.string.group_name_length));
                                    return;
                                }
                            }
                            saveGroupName(chat_id, name);
                        } else {
                            IMApplication.showToast(getResources().getString(
                                    R.string.input_group_name));
                            return;
                        }
                    }
                });
    }

    private void saveGroupName(final String chat_id, final String name) {
        loading.setVisibility(View.VISIBLE);

        mGroupMangerHandler.updateGroupInfo(chat_id, name, type,
                new GroupMangerHandler.IGroupResultListener() {

                    @Override
                    public void onGroupResult(int status, String msg) {
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            Intent intent = new Intent();
                            intent.putExtra("result", name);
                            setResult(RESULT_OK, intent);
                            Utils.hideSoftKeyboard(edit_group_info);
                            finish();
                        } else if (!TextUtils.isEmpty(msg)) {
                            IMApplication.showToast(msg);
                        }
                    }
                });
    }

    private InputFilter emojiFilter = new InputFilter() {
        Pattern emoji = Pattern
                .compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                return "";
            }
            return null;
        }
    };
}
